package com.lk;

import sun.security.krb5.internal.Ticket;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestLock {
    public static void main(String[] args) {
        DemoLock demoLock = new DemoLock();
        new Thread(demoLock,"1号窗口").start();
        new Thread(demoLock,"2号窗口").start();
        new Thread(demoLock,"3号窗口").start();
    }
}

class DemoLock implements Runnable{

    int ticketNum = 100;

    Lock lock = new ReentrantLock();

    @Override
    public void run() {
        lock.lock();
//        synchronized (this){
            try {
                while(true){
                    if(ticketNum>0){
                        try {
                            Thread.sleep(400);
                        } catch (InterruptedException e) {
                        }
                        ticketNum--;
                        System.out.println(Thread.currentThread().getName()+"  "+ticketNum);
                    }
                }
            }finally {
                lock.unlock();
            }
//        }
    }
}
